import router from '/@/router/index';

class util {
  // 路由跳转
  gotoPage(path, query){
    let params = {path: path};
    if(query){
        params.query = query;
    }
    router.push(params);
};
  transformArray(arr:Array<any>, idKey:any = 'value', nameKey:string = 'label'){
    let data = [];
    if(arr.length  > 0){
        data = arr.map(item => {
          if(Number(item[idKey]) >=0){
            item.id = Number(item[idKey]);
          }else{
            item.id = item[idKey];
          }
           
           item.name = item[nameKey];
           return item;
        });
    }else{
        data = []
    }
    return data;
  };
  selectName(val:any, arr:any[], keyId:string = 'id', keyName:string = 'name'){
    if(val == null){
      return null;
    }
    let obj = arr.find(item => item[keyId] == val);
    if(obj){
        return obj[keyName];
    }else{
        return null;
    }        
  };
  // 获取名字
  setName(id:any, list:any[], keyId:string = 'id', keyName:string='name') {
    let name = '';
    let arr = list.filter(item => item[keyId] == id);
    if(arr.length == 1){
        name = arr[0][keyName];
    }
    return name;          
  };
  resetForm(form1:any, form2:any){
    let obj = JSON.parse(JSON.stringify(form2));
    for(var key in form1){
        if(form2.hasOwnProperty(key)){
            form1[key] = obj[key];
        };
    }
  };
  getImg(name){
    return new URL('/src/assets/img/'+name+ '.jpg', import.meta.url).href;
  }
}

export default new util();